/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.transform_t2d.table;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.opu.doc2db.document.datadocument.ParagraphList;
import org.opu.doc2db.document.datadocument.Table;
import org.opu.doc2db.document.datadocument.TableCell;
import org.opu.doc2db.document.datadocument.TableRow;
import org.opu.doc2db.document.datadocument.TextParagraph;
import org.opu.vcforms.template.bean.Content;
import org.opu.vcforms.template.bean.SmartTable;
import org.opu.vcforms.transform_t2d.Processor;
import org.opu.vcforms.transform_t2d.SystemConfiguration;
import org.opu.vcforms.transform_t2d.Transformer;
import org.opu.vcforms.transform_t2d.content.ContentAPI;
import org.opu.vcforms.transform_t2d.content.ContentProcessor;
import org.opu.vcforms.transform_t2d.var.VarProcessor;

/**
 *
 * @author yura
 */
public class TableProcessor implements Processor<Table, SmartTable>{
    private Transformer transformer;

    public TableProcessor(Transformer transformer) {
        this.transformer = transformer;
    }

    @Override
    public Table transform(SmartTable smartTable)
            throws IllegalArgumentException, UnsupportedOperationException {
        
        Table table = new Table(smartTable.getId());

        for (ParagraphList object : smartTable) {
            // if it table row
            if (object instanceof TableRow){
                table.elementsAdd((TableRow) object);
            }

            // if it content in table
            else if (object instanceof Content){
                // get necessary variables
                Content cntnt = (Content) object;
                ContentAPI cP = transformer.getContentProcessor();
                Properties sS = transformer.getSystemSettings();
                SystemConfiguration sC = transformer.getSystemConfiguration();

                // create processors
                ContentProcessor cntPrc = new ContentProcessor(cP, null, cntnt);
                cntPrc.setTransformer(transformer);
                cntPrc.init();
                VarProcessor vp = new VarProcessor(cntPrc, sS, sC);

                // until we have next step (more repeatings)
                do{
                    // for each element in content
                    for (Object rowPrv : cntnt) {
                        
                        // if it element TableRow
                        if (rowPrv instanceof TableRow){
                            table.elementsAdd(processeRow(rowPrv, vp));
                        }

                        // if it not a row
                        else {
                            String err = "Content in table can have only rows.";
                            throw new UnsupportedOperationException(err);
                        }
                    }
                } while(cntPrc.hasData());
            }
            
            // it`s can`t be something else, but it`s not a reason fro exeption.
        }


        return table;
    }

    private TableRow processeRow(Object rowPrv, VarProcessor vp)
            throws IllegalArgumentException, UnsupportedOperationException {

        TableRow row = (TableRow) rowPrv;
        List<TableCell> cells = new ArrayList<TableCell>();

        // for each cell in this row
        for (TableCell cell : row) {

            List<TextParagraph> pars = new ArrayList<TextParagraph>();

            // check each paragraph in this cell
            for (TextParagraph par : cell) {
                pars.add(transformer.processeParagraph(par, vp));
            }

            // create new cell with processed paragraphs
            cells.add(new TableCell(cell.getId(), pars, cell.getWidth()));

        }

        // create new row with processed cells
        return new TableRow(row.getId(), cells, row.getHeight());
    }

}
